package com.cnseedlingscloud.draw.prize.service.impl;

import com.cnseedlingscloud.draw.prize.dao.TurntableDrawDao;
import com.cnseedlingscloud.draw.prize.dao.TurntablePhoneDao;
import com.cnseedlingscloud.draw.prize.dao.TurntableRecordDao;
import com.cnseedlingscloud.draw.prize.entity.TurntableDraw;
import com.cnseedlingscloud.draw.prize.entity.TurntablePhone;
import com.cnseedlingscloud.draw.prize.entity.TurntableRecord;
import com.cnseedlingscloud.draw.prize.service.PrizeService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    private TurntableDrawDao turntableDrawDao;
    @Autowired
    private TurntablePhoneDao turntablePhoneDao;
    @Autowired
    private TurntableRecordDao turntableRecordDao;


    //查询奖项
    @Override
    public List<TurntableDraw> queryAllDraw(){
       return turntableDrawDao.findAll();
    }
    //主键查询奖项
    @Override
    public TurntableDraw queryById(Long id) {
        return turntableDrawDao.findById(id);
    }
    //编辑
    @Override
    public void editDraw(TurntableDraw draw){
        turntableDrawDao.update(draw);
    }
    //删除奖项
    @Override
    public void delDraw(Long id){
        turntableDrawDao.delete(id);
    }

    //保存
    @Override
    @Transactional
    public void  saveDraw(TurntableDraw draw){
        if(draw.getWeight()==null){
            draw.setWeight(new BigDecimal(1));
        }
        turntableDrawDao.save(draw);
    }
    //中奖名单
    @Override
    public List<TurntableRecord> queryAllRecord(){
        return turntableRecordDao.queryAll();
    }
    //参与人数
    @Override
    public Integer countAll(){
        return turntablePhoneDao.countAll();
    }

    //奖项剩余数
    @Override
    public Integer queryRemain(Long id){
        return turntableRecordDao.queryRemain(id);
    }

    //查询参与人信息
    @Override
    public List<TurntablePhone> queryAllPhone(){
        return turntablePhoneDao.queryAll();
    }
    //剩余参与人员ID
    @Override
    public List<Long> queryByRemain(){
        return turntablePhoneDao.queryByRemain();
    }
    //保存中奖人员
    public  void saveLuckyNum(Long id, Long[] luckyArray){
        TurntableDraw draw = turntableDrawDao.findById(id);
        for(Long phoneId:luckyArray) {
            TurntablePhone turntablePhone=turntablePhoneDao.queryById(phoneId+1L);
            TurntableRecord turntableRecord=new TurntableRecord();
            turntableRecord.setDrawId(draw.getId());
            turntableRecord.setPrizeName(draw.getPrizeName());
            turntableRecord.setPhone(turntablePhone.getPhone());
            turntableRecordDao.save(turntableRecord);
        }
    }
}
